/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

#import "RCTBundleURLProvider.h"
#import "RCTRootView.h"
#import "../Libraries/LinkingIOS/RCTLinkingManager.h"

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件

#import "../Libraries/LinkingIOS/RCTLinkingManager.h"



#define kBaiduMap @"uh51Yq6wjHzUVNaIZXQw3G2kG5EjGGAR"

@interface AppDelegate ()<BMKLocationServiceDelegate>
{
  BMKMapManager* _mapManager;
  BMKLocationService *_locService;
}

@end


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  [self setUpBaiduMap];
  
//  jsCodeLocation = [NSURL URLWithString:@"http://192.168.19.153:8081/index.ios.bundle?platform=ios&dev=true"];
  
  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"WeiWeiGou"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  

  return YES;
}

-(void)initBaiduMapService{
  //初始化BMKLocationService
  _locService = [[BMKLocationService alloc]init];
  _locService.delegate = self;
  //启动LocationService
  [_locService startUserLocationService];
  
  NSString * lat =[NSString stringWithFormat:@"%lf", _locService.userLocation.location.coordinate.latitude];
  NSString * lon = [NSString stringWithFormat:@"%lf",_locService.userLocation.location.coordinate.longitude];
}

-(void)setUpBaiduMap{
  _mapManager = [[BMKMapManager alloc]init];
  // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
  BOOL ret = [_mapManager start:@"uh51Yq6wjHzUVNaIZXQw3G2kG5EjGGAR"  generalDelegate:nil];
  if (!ret) {
    NSLog(@"manager start failed!");
  }
}
//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
  //NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
  //NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
  NSString * lat =[NSString stringWithFormat:@"%lf", userLocation.location.coordinate.latitude];
  NSString * lon = [NSString stringWithFormat:@"%lf",userLocation.location.coordinate.longitude];
  NSArray * list = @[lat,lon];
  
  [[NSUserDefaults standardUserDefaults]setObject:list forKey:@"lacationList"];
  
}

- (void)applicationWillResignActive:(UIApplication *)application {
  [BMKMapView willBackGround];//当应用即将后台时调用，停止一切调用opengl相关的操作
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
  [BMKMapView didForeGround];//当应用恢复前台状态时调用，回复地图的渲染和opengl相关的操作
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

@end
